package com.tianfei.crowd.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianfei.crowd.entity.Role;
import com.tianfei.crowd.entity.RoleExample;
import com.tianfei.crowd.mapper.RoleMapper;
import com.tianfei.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Herz
 * @date: 2021/7/13 16:04
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 获取分页数据
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {

        // 1、开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 2、调用查询
        List<Role> roleList = roleMapper.selectRoleByKeyword(keyword);

        // 3、封装到PageInfo 对象
        return new PageInfo(roleList);
    }

    /**
     * 保存 角色
     * @param roleName
     */
    @Override
    public void saveRole(String roleName) {
        roleMapper.insert(new Role(null,roleName));
    }

    /**
     * 修改角色
     * @param roleId
     * @param roleName
     */
    @Override
    public void updateRole(Integer roleId, String roleName) {
        roleMapper.updateByPrimaryKeySelective(new Role(roleId,roleName));
    }

    /**
     * 根据id 数组删除角色
     * @param roleIdList
     */
    @Override
    public void deleteRole(List<Integer> roleIdList) {

        RoleExample example = new RoleExample();

        RoleExample.Criteria criteria = example.createCriteria();

        criteria.andIdIn(roleIdList);

        roleMapper.deleteByExample(example);
    }

    @Override
    public List<Role> getAssignedRole(Integer adminId) {
        return roleMapper.selectAssignedRole(adminId);
    }

    @Override
    public List<Role> getUnAssignedRole(Integer adminId) {
        return roleMapper.selectUnAssignedRole(adminId);
    }


}
